﻿using Model.ViewModel;
using WinForm.Modules;

namespace WinForm.Details
{
    public partial class CityAddEditDetail : DetailBase
    {
        //private 
        //public CityAddEditDetail()
        //    : base()
        //{
        //    InitializeComponent();
        //}
        //public CityAddEditDetail(Model.City city)
        //    : base()
        //{
        //    InitializeComponent();
        //    if (city == null) return;
        //    tbCityNo.Text = city.CityNo;
        //    tbCityName.Text = city.CityName;
        //}

        public City CurrentObject { get; set; }
        /// <summary>
        /// 必须
        /// </summary>
        /// <param name="praentModule"></param>
        /// <param name="city">添加时值为null，编辑时值为要编辑的对象</param>
        public CityAddEditDetail(ModuleBase praentModule, City city)
            : base(praentModule)
        {
            InitializeComponent();
            CurrentObject = city;
            if (city == null) return;
            tbCityNo.Text = city.CityNo;
            tbCityName.Text = city.CityName;
        }

        /// <summary>
        /// 保存数据，必须
        /// </summary>
        protected override void SaveData()
        {
            if (CurrentObject == null)
            {
                Model.City newCity = new Model.City
                {
                    CityNo = tbCityNo.Text.Trim(),
                    CityName = tbCityName.Text.Trim()
                };
                DAL.City.Add(newCity);
            }
            else
            {
                CurrentObject.CityNo = tbCityNo.Text.Trim();
                CurrentObject.CityName = tbCityName.Text.Trim();
                DAL.City.Save();
            }
        }

        /// <summary>
        /// 清空控件中的数据，必须
        /// </summary>
        protected override void ClearData()
        {
            tbCityNo.Text = string.Empty;
            tbCityName.Text = string.Empty;
            tbCityNo.Focus();
        }
         
    }
}
